package Merlion.userManagement.bean;

import Merlion.userManagement.entity.RoleEntity;
import Merlion.userManagement.entity.SystemUserEntity;
import Merlion.util.helper.PasswordHash;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Stateless
public class LoginBean implements LoginBeanRemote {

    @PersistenceContext
    private EntityManager em;
    //logger
    private static final Logger log = LoggerFactory.getLogger(LoginBean.class);

    public boolean isLocked(String email, String password) {
        System.out.println("**LoginBean: isLocked()");
        SystemUserEntity systemUser = em.find(SystemUserEntity.class, email);

        try {
            if (!systemUser.getPassword().equals(PasswordHash.MD5(password + email))) {
                int count = systemUser.getCountLock();
                count++;
                systemUser.setCountLock(count);
                if (count < 3) {
                    systemUser.setLockOutStatus(false);
                } else {
                    systemUser.setLockOutStatus(true);
                }
            }

            if (systemUser.isLockOutStatus() == true) {
                System.out.println("**LoginBean: Account has been locked. Please contact system administrator.");
                return true;
            } else {
                System.out.println("**LoginBean: Account is still available for login.");
                return false;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public boolean tryLogin(String email, String password) {
        System.out.println("**LoginBean: tryLogin()");
        SystemUserEntity systemUser = em.find(SystemUserEntity.class, email);

        try {
            if (systemUser == null) {
                System.out.println("**LoginBean: User is not registered yet. Please go to the registration page.");
                return false;
            } else if (!(systemUser.isAccountStatus())) {
                System.out.println("**LoginBean: Account is not activated yet. Please activate your account through your email.");
                return false;
            } else if (isLocked(email, password)) {
                System.out.println("**LoginBean: Account is locked. Contact technical support to reactivate your account.");
                return false;
            } else if (systemUser.getPassword().equals(PasswordHash.MD5(password + email))) {
                System.out.println("**LoginBean: Login is successful.");
                systemUser.setIsLogin(true);
                log.warn("User " + email + " has login.");
                return true;
            } else {
                System.out.println("**LoginBean: Wrong email and password combination. Try a different password.");
                return false;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public List<String> getRoles(String email) {
        System.out.println("**LoginBean: getRoles()");
        SystemUserEntity systemUser = em.find(SystemUserEntity.class, email);
        List<String> sRoles = new ArrayList<String>();

        Collection<RoleEntity> roles = systemUser.getRoles();

        for (Object o : roles) {
            RoleEntity r = (RoleEntity) o;
            String gRole = r.getRoles();
            System.out.println(gRole);
            sRoles.add(gRole);
        }

        System.out.println("**LoginBean: Listing default role...");
        return sRoles;
    }

    public boolean activateAccount(String email) {
        System.out.println("**LoginBean: activateAccount()");
        SystemUserEntity systemUser = em.find(SystemUserEntity.class, email);

        try {
            if (systemUser == null) {
                System.out.println("**LoginBean: Email cannot be found.");
                return false;
            } else {
                systemUser.setAccountStatus(true);
                System.out.println("**LoginBean: Account activated.");
                log.warn("User account " + email + " has been activated.");
                return true;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    public boolean tryLogout(String email) {
        System.out.println("**LoginBean: tryLogout()");
        SystemUserEntity systemUser = em.find(SystemUserEntity.class, email);

        try {
            if (systemUser != null) {
                systemUser.setIsLogin(false);
                System.out.println("**LoginBean: Logout is successful.");
                return true;
            } else {
                System.out.println(email + " is not logged in. Logout failed.");
                return false;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }
}
